package com.yuexun.dhdoctor.module.email.ui;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.email.adapter.MailListAdapter;
import com.yuexun.dhdoctor.module.email.bean.MailBean;
import com.yuexun.dhdoctor.module.email.bean.MailDataBean;
import com.yuexun.dhdoctor.module.email.model.impl.GetModel;
import com.yuexun.dhdoctor.module.email.presenter.impl.GetPresenter;
import com.yuexun.dhdoctor.module.email.ui.view.IGetView;
import com.yuexun.dhdoctor.widget.EmptyLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/8/3.
 */
public class Act_SendMailList extends BaseKJActivity implements IGetView {
    @Bind(R.id.filter_edittext)
    EditText filterEdittext;
    @Bind(R.id.getmail_list)
    PullToRefreshList getmailList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;
    @Bind(R.id.title_tv)
    TextView title_tv;

    private ListView mList;

    /**
     * 加载病人列表状态
     */
    public static final int DOWNLOAD_SUCCESS = 1;
    public static final int DOWNLOAD_NODATA = 0;
    public static final int DOWNLOAD_NETERROR = -1;

    GetPresenter presenter;

    ArrayList<MailDataBean> mailDataBeans = new ArrayList<MailDataBean>();
    MailListAdapter adapter;
    private int pageIndex =1;

    private int REFESH_TAG =1; // 1:重新初始化；2：添加

    @Override
    public void setRootView() {
        setContentView(R.layout.act_getmail);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        title_tv.setText("已发送");
        presenter = new GetPresenter(new GetModel(),this);
        listViewPreference();
        filterEdittext.setOnKeyListener(onKey);
    }

    @Override
    public void onResume()
    {
        super.onResume();
        initPresent();
    }

    private void initPresent(){
        REFESH_TAG = 1;
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("nameOrTitle",filterEdittext.getText().toString());
        params.put("pageIndex", pageIndex + "");
        params.put("type", "1");
        presenter.getMail_Draft(params);
    }
    View.OnKeyListener onKey=new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                initPresent();
            }
            return false;
        }
    };

    /**
     * 初始化ListView样式
     */
    private void listViewPreference() {
        mList = getmailList.getRefreshView();
        mList.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mList.setOverscrollFooter(null);
        mList.setOverscrollHeader(null);
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        getmailList.setPullLoadEnabled(false);//设置上拉刷新功能关闭
        ((FooterLoadingLayout) getmailList.getFooterLoadingLayout())
                .setNoMoreDataText("已经是最新状态");
        getmailList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
                initPresent();
            }

            @Override
            public void onPullUpToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
                getmailList.setHasMoreData(false);
            }
        });

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
//                presenter.showPatientList("");
            }
        });

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                Intent intent = new Intent(aty,Act_SendDetail.class);
                bundle.putSerializable("maillist",mailDataBeans);
                bundle.putInt("position", position);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @OnClick(R.id.back)
    public void onClick() {
        finish();
    }

    @Override
    public void showMailsList(MailBean list) {
        if(getmailList.isPullRefreshEnabled())
            getmailList.onPullDownRefreshComplete();
        if(REFESH_TAG == 1)
        {
            mailDataBeans.clear();
            mailDataBeans = list.getDatas();
            adapter = new MailListAdapter(mList,mailDataBeans,R.layout.item_maillist);
            mList.setAdapter(adapter);
            emptyLayout.dismiss();
        }
        else if(REFESH_TAG == 2){
            mailDataBeans.addAll(list.getDatas());
            adapter.notifyDataSetChanged();
            emptyLayout.dismiss();
        }
    }

    @Override
    public void PullDownRefreshState(int state) {
        switch (state) {
            case DOWNLOAD_SUCCESS:
                getmailList.onPullDownRefreshComplete();
                getmailList.onPullUpRefreshComplete();
                break;
            case DOWNLOAD_NETERROR:
                emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                break;
            case DOWNLOAD_NODATA:
                emptyLayout.setErrorType(EmptyLayout.NODATA);
        }
    }
}
